<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class addressController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 就近门店推荐
     */
    public function address(Request $request){
        try {
            //获取经纬度
            $lng = $request->get('lng');
            $lat = $request->get('lat');
            //调用第三方获取周边信息
            $message = file_get_contents("https://apis.map.qq.com/ws/place/v1/search?boundary=nearby($lat,$lng,1000)&keyword=商户&page_size=10&page_index=1&key=BF7BZ-HUCOP-6THDF-VEK5C-W6PVH-ZDFAT");
            $message=json_decode($message,true)['data'];
            //周边景点存入redis
            Redis::connect('139.196.231.1',6379);
            foreach ($message as $k=>$v){
                Redis::geoadd('address',$v['location']['lng'],$v['location']['lat'],$v['title']);
            }
            //计算两点排序
            $data =  Redis::georadius('address',$lng,$lat,'100','km','asc','withdist');
            return response()->json(['code'=>200,'msg'=>'排序成功','data'=>$data]);
        }catch (\Exception $exception){
            return response()->json(['code'=>2001,$exception->getMessage(),'data'=>'']);
        }
    }
}
